/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __IMovingClipMapTerrain_H__
#define __IMovingClipMapTerrain_H__

#pragma once

#include "IShaderEffect.h"
#include "IRenderer.h"
#include "ITerrain.h"

namespace terrain {

struct GrassPatch
{
	math::Vector3 position;
	float size;
};

class DLL_EXPORT IMovingClipMapTerrain :
	public terrain::ITerrain
{
public:
	static IMovingClipMapTerrain* Create();
	virtual ~IMovingClipMapTerrain(void);

	// Initializes a terrain with the specified (2^n + 1) vertices per side
	virtual void init(int n);

	// Returns the width of the terrain in units
	const int getTesselation() const;

	//math::Vector3 getNormal(float xPos, float yPos);

	// Sets a heightmap for the terrain
	virtual void setHeightmap(ITexture *pTexture);

	virtual void createGrassPatches(int numPatches, ITexture *texture) = 0;
	void calculateBoundingSphere();

protected:
	IMovingClipMapTerrain(void);

	unsigned int   m_uiTesselation;
	unsigned int   m_uiNumVertices;	
	
	math::Vector3  *m_pVertices;
	math::Vector2f *m_pTexCoords;
	math::Vector2f *m_pDetailTexCoords;

	WORD           *m_pIndices;		// 16-bit

	IShaderEffect  *m_pTerrainShader;
	ICamera        *m_pCamera;
	math::Vector2f  vInvTess;
	math::Vector2f  vTexSize;
	float           fOffsetScale;

	// Indicates if Render to Vertex Buffer should be used (for ATI graphic cards)
	bool m_bUseR2VB;

	IRenderer   *pRenderer;

	// Shader values
	SHADERHANDLE hTextureSize;
	SHADERHANDLE hOffset;
	SHADERHANDLE hDetailOffset;
	SHADERHANDLE hInvOffsetScale;
	SHADERHANDLE hInvTess;
	SHADERHANDLE hScaleFactor;
	// Shader Textures
	SHADERHANDLE hBlendMap;
	SHADERHANDLE hDetailMap1;
	SHADERHANDLE hDetailMap2;
	SHADERHANDLE hDetailMap3;
	SHADERHANDLE hHeightMap;
	SHADERHANDLE hNormalMap;
	SHADERHANDLE hGrassMap;
};

}

#endif
